package com.live.shop.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.live.shop.BaseActivity;
import com.live.shop.R;
import com.live.shop.model.CommentModel;
import com.live.shop.sql.CommentsListDatabase;
import com.live.shop.tool.StatusBarUtils;

import java.util.List;

public class LiveCommentsActivity extends BaseActivity {
    ImageButton live_comments_back;
    TextView live_comments_delete_nomark;
    TextView live_comments_delete_all;
    RecyclerView live_comments_recyclerview;
    CommentsListDatabase commentDb;
    List<CommentModel> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_comments);
        StatusBarUtils.setDarkStatusWhite(this,R.color.white);

        live_comments_back = findViewById(R.id.live_comments_back);
        live_comments_delete_nomark = findViewById(R.id.live_comments_delete_nomark);
        live_comments_delete_all = findViewById(R.id.live_comments_delete_all);
        live_comments_recyclerview = findViewById(R.id.live_comments_recyclerview);

        live_comments_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        live_comments_delete_nomark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                commentDb.deleteNoMark();
                list = commentDb.selectAll();
                adapter.notifyDataSetChanged();
            }
        });
        live_comments_delete_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                commentDb.deleteAll();
                list = commentDb.selectAll();
                adapter.notifyDataSetChanged();
            }
        });

        commentDb = new CommentsListDatabase(getApplicationContext());
        list = commentDb.selectAll();
        adapter = new MyAdapter();
        live_comments_recyclerview.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        live_comments_recyclerview.setAdapter(adapter);
    }
    private MyAdapter adapter;

    class MyAdapter extends RecyclerView.Adapter {
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyAdapter.MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment, parent, false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            MyAdapter.MyViewHolder mholder = (MyAdapter.MyViewHolder) holder;
            mholder.item_comment_info.setText(list.get(holder.getBindingAdapterPosition()).getCommentsUser()+"："+list.get(holder.getBindingAdapterPosition()).getCommentsTxt());
            mholder.item_comment_create_time.setText(list.get(holder.getBindingAdapterPosition()).getCreateTime());
            if(list.get(holder.getBindingAdapterPosition()).getCommentsMark() == 1){
                mholder.item_comment_mark.setImageResource(R.mipmap.icon_mark_true);
            }else{
                mholder.item_comment_mark.setImageResource(R.mipmap.icon_mark_false);
            }
            switch (list.get(holder.getBindingAdapterPosition()).getCommentsType()){
                case 1:mholder.item_comment_comments_type.setText("来了");break;
                case 2:mholder.item_comment_comments_type.setText("评论");break;
                case 3:mholder.item_comment_comments_type.setText("关注");break;
                case 4:mholder.item_comment_comments_type.setText("礼物");break;
            }
            switch (list.get(holder.getBindingAdapterPosition()).getKeywordMate()){
                case 0:mholder.item_comment_keyword_mate.setText("未匹配");mholder.item_comment_keyword_mate.setTextColor(getColor(R.color.gray_lite));break;
                case 1:mholder.item_comment_keyword_mate.setText("无匹配");mholder.item_comment_keyword_mate.setTextColor(getColor(R.color.gary_high));break;
                case 2:mholder.item_comment_keyword_mate.setText("已匹配");mholder.item_comment_keyword_mate.setTextColor(getColor(R.color.blue));break;
            }
            if(list.get(holder.getBindingAdapterPosition()).getKeywordReply() == 1){
                mholder.item_comment_keyword_reply.setText("已回复");
                mholder.item_comment_keyword_reply.setTextColor(getColor(R.color.blue));
            }else{
                mholder.item_comment_keyword_reply.setText("未回复");
                mholder.item_comment_keyword_reply.setTextColor(getColor(R.color.gray_lite));
            }

            mholder.item_comment_mark.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommentModel comModel = list.get(holder.getBindingAdapterPosition());
                    if(comModel.getCommentsMark() == 1){
                        comModel.setCommentsMark(0);
                    }else{
                        comModel.setCommentsMark(1);
                    }
                    commentDb.updateMark(comModel);
                    notifyDataSetChanged();
                }
            });
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView item_comment_info;
            ImageView item_comment_mark;
            TextView item_comment_comments_type;
            TextView item_comment_keyword_mate;
            TextView item_comment_keyword_reply;
            TextView item_comment_create_time;

            public MyViewHolder(View itemView) {
                super(itemView);
                item_comment_info = itemView.findViewById(R.id.item_comment_info);
                item_comment_mark = itemView.findViewById(R.id.item_comment_mark);
                item_comment_comments_type = itemView.findViewById(R.id.item_comment_comments_type);
                item_comment_keyword_mate = itemView.findViewById(R.id.item_comment_keyword_mate);
                item_comment_keyword_reply = itemView.findViewById(R.id.item_comment_keyword_reply);
                item_comment_create_time = itemView.findViewById(R.id.item_comment_create_time);
            }
        }
    }
}